package indoor.nexd.com.nexdmap.perimission;

import android.support.v4.app.ActivityCompat;

import indoor.nexd.com.nexdmap.ui.activity.LoadingActivity;
import permissions.dispatcher.PermissionUtils;

/**
 * 处理Loading页面的运行时权限问题
 * <p/>
 * Loading页面申请定位权限和文件的写入读入权限
 * <p/>
 * Created by wangxu on 16/4/1.
 */
public class LoadingActivitypermissionsDispatcher {
    private static final int REQUEST_STORAGE = 0;
    private static final int REQUEST_LOCATION = 1;
    private static final int REQUEST_PHONE = 2;

    private static final String[] PERMISSION_STORAGE = new String[]{"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE"};
    private static final String[] PERMISSION_LOCATION = new String[]{"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"};
    private static final String[] PERMISSION_PHONE = new String[]{"android.permission.READ_PHONE_STATE"};

    private LoadingActivitypermissionsDispatcher() {
    }

    public static void showPhoneWithCheck(LoadingActivity target) {
        if (PermissionUtils.hasSelfPermissions(target, PERMISSION_PHONE)) {
            target.uploadOperaRecord();
        } else {
//            if (PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_PHONE)) {
//                target.showRationaleForPhone(new ShowPhonePermissionRequest(target));
//            } else {
                ActivityCompat.requestPermissions(target, PERMISSION_PHONE, REQUEST_PHONE);
//            }
        }
    }

    public static void showStorgeWithCheck(LoadingActivity target) {
        if (PermissionUtils.hasSelfPermissions(target, PERMISSION_STORAGE)) {
            target.checkPhonePermission();
        } else {
//            if (PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_STORAGE)) {
//                target.showRationaleForStorage(new ShowStoragePermissionRequest(target));
//            } else {shouldShowRequestPermissionRationale
                ActivityCompat.requestPermissions(target, PERMISSION_STORAGE, REQUEST_STORAGE);
//            }
        }
    }

    public static void showLocationWithCheck(LoadingActivity target) {
        if (PermissionUtils.hasSelfPermissions(target, PERMISSION_LOCATION)) {
            target.locationCurrentPermission();
        } else {
//            if (PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_LOCATION)) {
//                target.showRationaleForLocation(new ShowLocationPermissionRequest(target));
//            } else {
                ActivityCompat.requestPermissions(target, PERMISSION_LOCATION, REQUEST_LOCATION);
//            }
        }
    }


    public static void onRequestPermissionsResult(LoadingActivity target, int requestCode, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_STORAGE:
                if (PermissionUtils.getTargetSdkVersion(target) < 23 && !PermissionUtils.hasSelfPermissions(target, PERMISSION_STORAGE)) {
                    target.onStorageDenied();
                    return;
                }
                if (PermissionUtils.verifyPermissions(grantResults)) {
                    target.checkPhonePermission();

                } else {
//                    if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_STORAGE)) {
//                        target.onStorageNeverAskAgain();
//                    } else {
                        target.onStorageDenied();
//                    }
                }
                break;

            case REQUEST_LOCATION:
                if (PermissionUtils.getTargetSdkVersion(target) < 23 && !PermissionUtils.hasSelfPermissions(target, PERMISSION_LOCATION)) {
                    target.onLocationDenied();
                    return;
                }
                if (PermissionUtils.verifyPermissions(grantResults)) {
                    target.locationCurrentPermission();
                } else {
//                    if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_LOCATION)) {
//                        target.onLocationNeverAskAgain();
//                    } else {
                        target.onLocationDenied();
//                    }
                }
                break;
            case REQUEST_PHONE:
                if (PermissionUtils.getTargetSdkVersion(target) < 23 && !PermissionUtils.hasSelfPermissions(target, PERMISSION_PHONE)) {
                    target.onPhoneDenied();
                    return;
                }
                if (PermissionUtils.verifyPermissions(grantResults)) {
                    target.uploadOperaRecord();
                } else {
//                    if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_PHONE)) {
//                        target.onPhoneNeverAskAgain();
//                    } else {
                        target.onPhoneDenied();
//                    }
                }
                break;
            default:
                break;
        }
    }

//    public static final class ShowPhonePermissionRequest implements PermissionRequest {
//
//        private final WeakReference<LoadingActivity> weakReference;
//
//        public ShowPhonePermissionRequest(LoadingActivity target) {
//            this.weakReference = new WeakReference<>(target);
//        }
//
//        @Override
//        public void proceed() {
//            LoadingActivity target = weakReference.get();
//            if (target == null) return;
//            ActivityCompat.requestPermissions(target, PERMISSION_PHONE, REQUEST_PHONE);
//        }
//
//        @Override
//        public void cancel() {
//            LoadingActivity target = weakReference.get();
//            if (target == null) return;
//            target.onPhoneDenied();
//        }
//    }
//
//
//    public static final class ShowStoragePermissionRequest implements PermissionRequest {
//        private final WeakReference<LoadingActivity> weakTarget;
//
//        private ShowStoragePermissionRequest(LoadingActivity target) {
//            this.weakTarget = new WeakReference<>(target);
//        }
//
//        @Override
//        public void proceed() {
//            LoadingActivity target = weakTarget.get();
//            if (target == null) return;
//            ActivityCompat.requestPermissions(target, PERMISSION_STORAGE, REQUEST_STORAGE);
//        }
//
//        @Override
//        public void cancel() {
//            LoadingActivity target = weakTarget.get();
//            if (target == null) return;
//            target.onStorageDenied();
//        }
//    }
//
//    public static final class ShowLocationPermissionRequest implements PermissionRequest {
//
//        private final WeakReference<LoadingActivity> weakTarget;
//
//        public ShowLocationPermissionRequest(LoadingActivity weakTarget) {
//            this.weakTarget = new WeakReference<>(weakTarget);
//        }
//
//        @Override
//        public void proceed() {
//            LoadingActivity target = weakTarget.get();
//            if (target == null) return;
//            ActivityCompat.requestPermissions(target, PERMISSION_LOCATION, REQUEST_LOCATION);
//        }
//
//        @Override
//        public void cancel() {
//            LoadingActivity target = weakTarget.get();
//            if (target == null) return;
//            target.onLocationDenied();
//        }
//    }


}
